package com.sunyur.tailor.channel.service.inner.invoice.service.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.base.SunyurEventService;
import com.sunyur.tailor.channel.service.inner.invoice.model.InvoiceHeadModel;
import com.sunyur.tailor.channel.service.inner.invoice.model.InvoiceItemModel;
import com.sunyur.tailor.channel.service.inner.invoice.param.InvoiceHeadParam;
import com.sunyur.tailor.channel.service.inner.invoice.param.InvoiceItemParam;
import com.sunyur.tailor.channel.service.inner.invoice.service.SunyurInvoiceService;
import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
/**
 * @Author pengbing
 * @Description //发票确认消息
 * @Date 17:08 2021/6/1
 * @Param [sendModel]
 * @return com.alibaba.fastjson.JSONObject
 **/
public class SunyurInvoiceEntranceServiceImpl implements SunyurEventService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurInvoiceEntranceServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurInvoiceEntranceServiceImpl.class);

    @Autowired
    private SunyurInvoiceService sunyurInvoiceService;

    public SunyurResult<Boolean> confirm(String param) throws Exception {
        LOGGER_INFO.info("SunyurInvoiceEntranceServiceImpl deliverPurchaseTeam param:{}", param);
        SunyurResult<Boolean> result = new SunyurResult<>();
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        } else {
            String code = JSON.parseObject(param, MsgResultCode.class).getCode();
            //获取发票头
            InvoiceHeadParam invoiceHeadParam = new InvoiceHeadParam();
            invoiceHeadParam.setInvoiceOrderCode(code);
            ChannelResult<InvoiceHeadModel> invoiceHead = sunyurInvoiceService.getInvoice(invoiceHeadParam);
            if (invoiceHead.getContent() == null) {
                result.setErrorMessage(invoiceHead.getCode(),invoiceHead.getMessage());
                return result;
            }


            //发票行信息
            InvoiceItemParam invoiceItemParam = new InvoiceItemParam();
            invoiceItemParam.setInvoiceOrderCode(code);
            ChannelResult<List<InvoiceItemModel>> invoiceItemLit = sunyurInvoiceService.listInvoiceOrderItem(invoiceItemParam);
            if (invoiceItemLit.getContent() == null) {
                result.setErrorMessage(invoiceItemLit.getCode(),invoiceItemLit.getMessage());
                return result;
            }

        }

        result.setCode("00000");
        return result;
    }
}